package com.qf.team2.netdisc.controller;

import com.alipay.api.AlipayApiException;
import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.entity.AlipayOrder;
import com.qf.team2.netdisc.common.pojo.qo.AlipayOrderQO;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import com.qf.team2.netdisc.service.AlipayOrderService;
import com.qf.team2.netdisc.service.UserService;
import com.qf.team2.netdisc.service.impl.CancelOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 支付宝支付接口
 *
 * @author Simon_Wong
 * @date 2021/05/25 16:36
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Api(tags = "支付宝支付接口")
@RestController("/order")
@RequestMapping("/order")
public class AlipayOrderController extends BaseController<Object> {
    @Autowired
    private AlipayOrderService alipayOrderService;

    @Autowired
    private CancelOrderService cancelOrderService;

    @Autowired
    private UserService userService;

    @ApiOperation("订单支付")
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public void alipay(AlipayOrderQO alipayOrderQO, HttpServletResponse resp) throws AlipayApiException, IOException {
        //将订单加入数据库
        alipayOrderService.addOrder(alipayOrderQO);
        //创建支付宝订单对象
        AlipayOrder alipayOrder = new AlipayOrder();
        alipayOrder.setOut_trade_no(alipayOrderQO.getOutTradeNo());
        alipayOrder.setSubject(alipayOrderQO.getSubject());
        alipayOrder.setTotal_amount(alipayOrderQO.getTotalAmount());
        alipayOrder.setBody(alipayOrderQO.getBody());
        Cookie cookie = new Cookie("out_trade_no",alipayOrder.getOut_trade_no());
        cookie.setPath("/");
        cookie.setMaxAge(10*60*60);
        resp.addCookie(cookie);
        //执行支付进程
        resp.setHeader("content-type", "text/html; charset=UTF-8");
        resp.getWriter().write(alipayOrderService.aliPay(alipayOrder));
    }

    @ResponseBody
    @ApiOperation("更新订单状态")
    @RequestMapping(value = "/paid", produces = "application/json", method = RequestMethod.POST)
    public RespResultVO<Object> paid(HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
        Integer userId = null;
        Integer orderId=null;
        for (Cookie cookie : cookies) {
            if ("userId".equals(cookie.getName())) {
                userId = Integer.valueOf(cookie.getValue());
            }
            if("out_trade_no".equals(cookie.getName())){
                orderId=Integer.valueOf(cookie.getValue());
                cookie.setMaxAge(-1);
            }
        }

        if(null == orderId){
            return failedResp("状态不明");
        }

        int i = cancelOrderService.insertVip(userId);
        if(1==i){
            return failedResp("修改VIP状态失败");
        }

        alipayOrderService.setOrderStatus(orderId, 1);
        return successResp(userService.setUserVIP(userId), "恭喜您成为会员");
    }

}